/*==============================================================================
	GEOGRAPHIC MAPPING OF NONBANK LENDING SHARE BY DANISH MUNICIPALITIES
	
	This do-file creates maps showing the share of nonbank lending
	across Danish municipalities for both firm and household credit using 
	Statistics Denmark (DST) data and Eurostat's Local Administrative Units 
	(LAU) shapefiles. Files are run outside the DST environment as shape
	files were not available to us.
	
	Data Inputs:
	- Household-level data: nonbankshare_HHs_bymunicipality.dta
		- Generated by 02_descr_1_individuals.do on DST server
	- Firm-level data: nonbankshare_firms_bymunicipality.dta
		- Generated by 02_descr_2_firms.do on DST server
	- Geographic boundaries: Eurostat LAU_RG_01M_2019_3035 shapefiles
	
	Output: 
	- Map of firm nonbank lending share by municipality (map_firm_credit.eps)
	- Map of household nonbank lending share by municipality (map_HHs_credit.eps)
==============================================================================*/

clear all

// Set project paths
global projectpath "YOUR OFFLINE PATH"  // path outside the DST environment

global figures "${projectpath}\figures"
global tables "${projectpath}\tables"
global procdata "${projectpath}\processed_data"
global rawdata "${projectpath}\raw_data"


/*------------------------------------------------------------------------------
	STEP 1: INSTALL REQUIRED PACKAGES AND PREPARE GEOGRAPHIC DATA
------------------------------------------------------------------------------*/

// Install spatial mapping package (run once)
ssc install spmap

// Convert Eurostat shapefiles to Stata format
// Note: LAU_RG_01M_2019_3035 contains EU Local Administrative Units boundaries
spshape2dta "${rawdata}/LAU_RG_01M_2019_3035", saving(EUdb) replace

/*------------------------------------------------------------------------------
	STEP 2: PREPARE FIRM-LEVEL NONBANK LENDING MAP
------------------------------------------------------------------------------*/

// Import firm-level data aggregated by municipality from Statistics Denmark
import excel using "${tables}/nonbankshare_firms_bymunicipality", firstrow clear

// Standardize municipality identifier to match Eurostat LAU codes
rename Kommunekodeanno2007 LAU_ID
tostring LAU_ID, replace

// Save prepared municipal data
save "${procdata}/muni_dst_firms.dta", replace

// Load converted geographic database
use "./EUdb.dta", clear

// Restrict to Danish municipalities only
keep if CNTR_CODE == "DK"

// Merge with firm-level nonbank share data
joinby LAU_ID using "${procdata}/muni_dst_firms.dta", unmatched(both)

// Verify merge quality
tab _merge
drop if _merge != 3  // Keep only successfully matched observations
drop _merge

// Convert nonbank share to percentage scale
replace share_nonbank = share_nonbank * 100

// Format for display
format share_nonbank %12.0f

// Generate firm credit map with high-contrast grayscale scheme
spmap share_nonbank using EUdb_shp, ///
	id(_ID) ///
	fcolor(gs16 gs12 gs8 gs4 gs0) ///
	legstyle(1) ///
	legend(position(2) size(large) title("Nonbank Share (%)")) ///
	cln(5) ///
	ocolor(black) ///
	osize(vthin) ///
	name(map_firm, replace)

// Export firm credit map
graph export "${figures}/map_firm_credit.eps", ///
    replace fontface("Arial") mag(133)

/*------------------------------------------------------------------------------
	STEP 3: PREPARE HOUSEHOLD-LEVEL NONBANK LENDING MAP
------------------------------------------------------------------------------*/

// Clear workspace and prepare for household data
clear

// Import household-level data aggregated by municipality from Statistics Denmark
import excel using "${tables}/nonbankshare_HHs_bymunicipality", firstrow clear

// Standardize municipality identifier to match Eurostat LAU codes
rename municipality LAU_ID
tostring LAU_ID, replace

// Save prepared municipal household data
save "${procdata}/muni_dst_households.dta", replace

// Load converted geographic database
use "./EUdb.dta", clear

// Restrict to Danish municipalities only
keep if CNTR_CODE == "DK"

// Merge with household-level nonbank share data
joinby LAU_ID using "${procdata}/muni_dst_households.dta", unmatched(both)

// Verify merge quality
tab _merge
drop if _merge != 3  // Keep only successfully matched observations
drop _merge

// Convert nonbank share to percentage scale
replace share_nonbank = share_nonbank * 100

// Format for display
format share_nonbank %12.0f

// Generate household credit map with high-contrast grayscale scheme
spmap share_nonbank using EUdb_shp, ///
	id(_ID) ///
	fcolor(gs16 gs12 gs8 gs4 gs0) ///
	legstyle(1) ///
	legend(position(2) size(large) title("Nonbank Share (%)")) ///
	cln(5) ///
	ocolor(black) ///
	osize(vthin) ///
	name(map_HHs, replace)

// Export household credit map
graph export "${figures}/map_HHs_credit.eps", ///
    replace fontface("Arial") mag(133)

/*------------------------------------------------------------------------------
	STEP 4: CLEAN UP TEMPORARY FILES
------------------------------------------------------------------------------*/

// Remove temporary datasets
cap erase "${procdata}/muni_dst_firms.dta"
cap erase "${procdata}/muni_dst_households.dta"

/*------------------------------------------------------------------------------
	END OF DO-FILE
------------------------------------------------------------------------------*/